package com.huellas.servlet.cita;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huellas.excepcion.DAOExcepcion;
import com.huellas.negocio.GestionCatalogoTarea;
import com.huellas.negocio.GestionCitaCatalogo;
import com.huellas.negocio.GestionDoctor;
import com.huellas.negocio.GestionPacientes;

/**
 * Servlet implementation class CrearCitaTareaServlet
 */
@WebServlet("/CrearCitaTareaServlet")
public class CrearCitaTareaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CrearCitaTareaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		int CoCita = Integer.parseInt(request.getParameter("CoCita")); 
		String FeCita = request.getParameter("FeCita"); 
		String FeHora = request.getParameter("FeHora") + ":00";
		int CoCliente = Integer.parseInt(request.getParameter("CoCliente"));
		String NoPaciente = request.getParameter("NoPaciente");
		String NoDoctor = request.getParameter("NoDoctor");
		String NoTarea = request.getParameter("NoTarea");
		
		String FlTipoCita = "tarea";
		int CoEstadoCita = 1;
		int CoPaciente = 0;
		int CoDoctor = 0;
		int CoCatalogoTarea = 0;
		
		try{
			
			GestionPacientes gestionPaciente = new GestionPacientes();
			CoPaciente = gestionPaciente.obtenerCoPaciente(NoPaciente, CoCliente);
			
			GestionDoctor gestionDoctor = new GestionDoctor();
			CoDoctor = gestionDoctor.obtenerCoDoctor(NoDoctor);
			
			GestionCatalogoTarea gestionCatalogoTarea = new GestionCatalogoTarea();
			CoCatalogoTarea = gestionCatalogoTarea.obtenerCoCatalogoTarea(NoTarea);
			
			GestionCitaCatalogo gestionCitaCatalogo = new GestionCitaCatalogo();
			
			if (gestionCitaCatalogo.validarRegistroTarea(CoCatalogoTarea, FeCita, FeHora, FlTipoCita, CoEstadoCita, CoPaciente, CoDoctor) == true){
				gestionCitaCatalogo.insertarCitaCatalogo(CoCita, FeCita, FeHora, FlTipoCita, CoEstadoCita, CoPaciente, CoDoctor, CoCatalogoTarea);
				RequestDispatcher requestDispatcher = request.getRequestDispatcher("confirmacion.jsp");
				requestDispatcher.forward(request, response);
			}else{
				RequestDispatcher requestDispatcher = request.getRequestDispatcher("duplicado.jsp");
				requestDispatcher.forward(request, response);
			}
		}catch(DAOExcepcion e){
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("error.jsp");
			requestDispatcher.forward(request, response);
		}

	}

}
